import { defineStore } from "pinia";

import { autoGetIP, getIPValue } from "@/services/public";

const usePublicStore = defineStore("public", {
  state: () => ({
    city: "",
    code: "",
    showSearch: false,
  }),
  actions: {
    // 第一次自动获取ip
    async fetchIP() {
      if (this.city.length <= 0) {
        const res1 = await autoGetIP();

        const res2 = await getIPValue(res1.data.adcode);
        this.city = res2.data.location[0].name;
        this.code = res2.data.location[0].id;
      }
    },
    async fetchChangePublic(city, code) {
      this.city = city;
      this.code = code;
    },
  },
});

export default usePublicStore;
